package com.gopay.bis.ipay.ebank.operation;

import java.util.HashMap;
import java.util.Map;

import javax.naming.CommunicationException;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.ipay.ebank.util.PlainTextUtil;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.ipay.ebank.IpayMessage;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.HttpTransport;
import com.gopay.remote.bank.ICertHelper;

/**
 * 对账文件
 * @author lizheng
 *
 */
public class IpayDayEnd {
	// 签名验签处理类
	private ICertHelper ipayCertHelper;
	private String billListUrl;

	protected Logger log = LoggerFactory.getLogger(IpayDayEnd.class);

	/**
	 * 
	 * @param message
	 * @return
	 * @throws GopayException
	 */
	public IpayMessage dayEnd(IpayMessage message) throws GopayException {

		// 构建请求参数
		Map<String, String> dayEndReq = buildDayEndReq(message);

		// 通讯
		Object transportResult = dayEndTransport(dayEndReq);

		// 解析查询结果
		IpayMessage ret = parseDayEndRes(message,transportResult);

		return ret;
	}

	/**
	 * 
	 * @param param
	 * @return
	 * @throws GopayException
	 */
	public Object dayEndTransport(Map<String, String> param)
			throws GopayException {
		try {
			HttpTransport httpTransport = new HttpTransport();
			httpTransport.setUrl(billListUrl);
			Object transportResult = (Object) httpTransport.submit(param);
			log.error("dayEnd transport result: " + transportResult);
			return transportResult;
		} catch (CommunicationException e) {
			log.error("IpayDayEnd error", e);
			throw new GopayException(ProcCodeConstants.PROC_CODE_300G1011);
		}
	}

	/**
	 * 
	 * @param message
	 * @return
	 * @throws GopayException
	 */
	public Map<String, String> buildDayEndReq(IpayMessage message)
			throws GopayException {
		String signMessage = (String) ipayCertHelper.sign(message);
		Map<String, String> param = new HashMap<String, String>();
		param.put("oid_partner", message.getOidPartner());
		param.put("settle_date", message.getSettleDate());
		param.put("sign", signMessage);
		log.info("build dayEnd req: " + param);
		return param;
	}

	/**
	 * 
	 * @param object
	 * @return
	 * @throws GopayException
	 */
	public IpayMessage parseDayEndRes(IpayMessage message ,Object object) throws GopayException {
		JSONObject jsonObject = JSONObject.fromObject((String) object);
		message.setRespType(jsonObject.getString("resp_type"));
		message.setRespCode(jsonObject.getString("resp_code"));
		message.setRespMsg(jsonObject.getString("resp_msg"));
		message.setContent(jsonObject.getString("content"));
		message.setSignMessage(jsonObject.getString("sign"));
		message.setSettleDate(jsonObject.getString("settle_date"));
		message.setOidPartner(jsonObject.getString("oid_partner"));
		message.setPlainMessage(PlainTextUtil.getPlainText(jsonObject));
		// 验签
		boolean flag = (Boolean) ipayCertHelper.verify(message);
		message.setFlag(flag);
		log.info("parse dayEnd res: " + message);
//		if (!flag) {
//			throw new GopayException(ProcCodeConstants.PROC_CODE_100E6010);
//		}
		return message;
	}

	public void setIpayCertHelper(ICertHelper ipayCertHelper) {
		this.ipayCertHelper = ipayCertHelper;
	}

	public void setBillListUrl(String billListUrl) {
		this.billListUrl = billListUrl;
	}

}
